package com.tjtt.gwm.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tjtt.common.utils.TotalFlowXmlTool;
import com.tjtt.gwm.bean.Junyans;
import com.tjtt.gwm.bean.Junyansceshis;
import com.tjtt.gwm.bean.Junyansshejis;
import com.tjtt.gwm.service.DbService;
import com.tjtt.gwm.service.EquipmentService;
import com.tjtt.gwm.service.GuanxianService;
import com.tjtt.gwm.service.JunyansService;
import com.tjtt.gwm.service.SearchService;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.codeReview.service.ICodeReviewService;
import com.tjtt.tdm.flow.bean.FlowDetailed;
import com.tjtt.tdm.flow.service.IFlowformService;
import com.tjtt.tdm.util.PubMethod;
import com.tjtt.tdm.util.WordTool;

public class SearchAction extends BaseAction {
	private DbService dbService;
	private SearchService searchService;
	public DbService getDbService() {
		return dbService;
	}
	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}
	public SearchService getSearchService() {
		return searchService;
	}
	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}
	//日期查询字段
    public void getFieldnameDateSearch() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		String owner=getRequest().getParameter("owner");
		JSONArray jsonArray = new JSONArray();
		String strSql = "select t.CONDITION_FIELD_EN,t.CONDITION_FIELD_CN " +
				"from ptem_search_date t where t.owner='"+owner+"'";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[0]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	//区间查询字段
    public void getFieldnameRange() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		String owner=getRequest().getParameter("owner");
		JSONArray jsonArray = new JSONArray();
		String strSql = "select t.CONDITION_FIELD_EN,t.CONDITION_FIELD_CN " +
				"from ptem_search_range t where t.owner='"+owner+"'";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			//jsonobj.put("id", objs[0]);
			jsonobj.put("id", objs[1]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	//选择查询字段
    public void getFieldnameSelect() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		String owner=getRequest().getParameter("owner");
		JSONArray jsonArray = new JSONArray();
//		String strSql = "select t.CONDITION_FIELD_EN,t.CONDITION_FIELD_CN " +
//				"from ptem_search_select t where t.owner='"+owner+"'";
		String strSql = "select t.DICTIONARY_TABLE,t.CONDITION_FIELD_CN," +
				"t.CONDITION_FIELD_VALUE,t.CONDITION_TABLE_CN " +
		"from ptem_search_select t where t.owner='"+owner+"'";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			if(objs[1]==null) //没有条件字段
			{
				jsonobj.put("id", objs[3]);
				jsonobj.put("text", objs[3]);
			}
			else if(objs[2]!=null) //含有字段值
			{
				jsonobj.put("id", objs[2]);
				jsonobj.put("text", objs[2]);
			}
			else
			{
				jsonobj.put("id", objs[0]);
				jsonobj.put("text", objs[1]);
			}
			
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	//关键字查询字段
    public void getFieldnameKeyword() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		String owner=getRequest().getParameter("owner");
		JSONArray jsonArray = new JSONArray();
		String strSql = "select t.CONDITION_FIELD_EN,t.CONDITION_FIELD_CN " +
				"from ptem_search_keyword t where t.owner='"+owner+"'";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[0]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
		
   //关键字查询字段
   public void getFieldname1s() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		String strSql = "select t.fieldname,t.fieldcnname from ptem_fieldname1 t";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[0]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	
	public void getTablenames() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		String strSql = "select t.tablename,t.tablecnname from ptem_tablename t";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[0]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	
	public void getFieldnameDictionary() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		String tablename = getRequest().getParameter("tablename");
		String strSql;
		List lists;
		int length=tablename.length();
		if(length<9)
		{
			tablename="PTEM_TYPE_HAS";
		}
		else
		{
			String temp=tablename.substring(0, 9);
			if(temp.equals("PTEM_TYPE")==false)
			{
				tablename="PTEM_TYPE_HAS";
			}
		}
		
		if(tablename.equals("PTEM_TYPE_HAS"))
		{
			strSql = "select t.HASNAME_CN,t.HASNAME_EN from "+tablename+" t";
			lists = dbService.queryDataBySql(strSql);
			for(int i=0;i<lists.size();i++){
				Object[] objs = (Object[])lists.get(i);
				JSONObject jsonobj = new JSONObject();
				jsonobj.put("id", objs[1]);
				jsonobj.put("text", objs[0]);
				jsonArray.add(jsonobj);
			}
		}
		//检测项目
		else if(tablename.equals("PTEM_TYPE_TESTITEM"))
		{
			strSql = "select t.typename,t.tablename from "+tablename+" t";
			lists = dbService.queryDataBySql(strSql);
			for(int i=0;i<lists.size();i++){
				Object[] objs = (Object[])lists.get(i);
				JSONObject jsonobj = new JSONObject();
				jsonobj.put("id", objs[1]);
				jsonobj.put("text", objs[0]);
				jsonArray.add(jsonobj);
			}
		}
		//特征点类型
		else if(tablename.equals("PTEM_TYPE_POINT_QUERY"))
		{
			strSql = "select t.typename,t.tablename from "+tablename+" t";
			lists = dbService.queryDataBySql(strSql);
			for(int i=0;i<lists.size();i++){
				Object[] objs = (Object[])lists.get(i);
				JSONObject jsonobj = new JSONObject();
				jsonobj.put("id", objs[0]);
				jsonobj.put("text", objs[0]);
				jsonArray.add(jsonobj);
			}
		}
		else
		{
			strSql = "select t.typename,t.typeindex from "+tablename+" t";
			lists = dbService.queryDataBySql(strSql);
			for(int i=0;i<lists.size();i++){
				Object[] objs = (Object[])lists.get(i);
				JSONObject jsonobj = new JSONObject();
				jsonobj.put("id", objs[0]);
				jsonobj.put("text", objs[0]);
				jsonArray.add(jsonobj);
			}
		}	
		out.print(jsonArray.toString());
	}
	public void getFieldname2s() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		String tablename = getRequest().getParameter("tablename");
		String strSql = "select t.typename,t.typeindex from "+tablename+" t";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[1]);
			jsonobj.put("text", objs[0]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	//区间查询字段
	public void getFieldname3s() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		String strSql = "select t.fieldname,t.fieldcnname from ptem_fieldname3 t";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[0]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	//区间
	public void getFieldname4s() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		String strOwner=getRequest().getParameter("owner");
		String strSql = "select t.limitname,t.limitcnname" +
				" from ptem_searchrange t" +
				" where t.owner='"+strOwner+"'";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[0]);
			jsonobj.put("text", objs[1]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
	//日期类型
	public void getFieldnameDate() throws IOException{
		PrintWriter out=PubMethod.GetPrintWriter();
		JSONArray jsonArray = new JSONArray();
		
		String strSql = "select t.fieldcnname,t.fieldname from ptem_datesearch t";
		List lists = dbService.queryDataBySql(strSql);
		for(int i=0;i<lists.size();i++){
			Object[] objs = (Object[])lists.get(i);
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("id", objs[1]);
			jsonobj.put("text", objs[0]);
			jsonArray.add(jsonobj);
		}
		out.print(jsonArray.toString());
	}
}
